package com.piece.test;

import com.piece.core.elasticsearch.properties.ElasticsearchProperties;
import com.piece.core.elasticsearch.support.ESClientPool;
import com.piece.core.elasticsearch.support.EsClientPoolFactory;
import com.piece.core.elasticsearch.support.EsSearchUtil;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ElasticsearchTest {

    private ESClientPool getClientPool() {
        ElasticsearchProperties properties = new ElasticsearchProperties();
        properties.setHosts(Arrays.asList("elasticsearch.piece-cloud:9200"));
        return new ESClientPool(new EsClientPoolFactory(properties));
    }

    @Test
    public void insertDocument() {
        Article article = new Article();
        article.setArticleId(1L);
        article.setTitle("测试标题");
        article.setSummary(null);
        article.setWeight(1);
        article.setContent("这是一篇测试文章的内容，主要用来测试验证");
        article.setHtml("这是一篇测试文章的内容，主要用来测试验证");
        try {
            EsSearchUtil.getInstance(getClientPool()).insert(article);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void updateDocument() {
        Article article = new Article();
        article.setArticleId(1L);
        article.setTitle("测试标题被修改了");
        article.setSummary(null);
        article.setWeight(1);
        article.setContent("这是一篇测试文章的内容，主要用来测试验证");
        article.setHtml("这是一篇测试文章的内容，主要用来测试验证");
        try {
            EsSearchUtil.getInstance(getClientPool()).update(article);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void deleteIndex() {
        try {
            EsSearchUtil.getInstance(getClientPool()).delete(Article.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void searchDocument() {
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("Q_title_S_LK_OR", "测试");
            param.put("Q_content_S_LK_OR", "测试");
            List searchList = EsSearchUtil.getInstance(getClientPool())
                    .searchList(param, null, true, Article.class);
            searchList.stream().forEach(s -> System.out.println(s.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
